package com.dingshi.security.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dingshi.security.R;
import com.dingshi.security.util.Utils;

public class UpdateDialog extends Dialog implements View.OnClickListener{

	private Context context;
	private OnClickListener onPositiveClickListener;
	private OnClickListener onNegativeClickListener;
	private Button btn_confirm;
	private Button btn_cancel;
	private String version;
	private String desc;

	private UpdateDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		init(context);
	}

	public UpdateDialog(Context context, String version,String desc) {
		super(context);
		this.version = version;
		this.desc = desc;
		init(context);
	}

	public UpdateDialog(Context context) {
		super(context);
		init(context);
	}

	private void init(Context context) {
		this.context = context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(
				R.layout.update, null);
//		linearLayout.setBackgroundResource(R.drawable.bg_toast);
		TextView text = (TextView)linearLayout.findViewById(R.id.textView1);
		btn_confirm = (Button)linearLayout.findViewById(R.id.button1);
		btn_cancel = (Button)linearLayout.findViewById(R.id.button2);
		btn_confirm.setOnClickListener(this);
		btn_cancel.setOnClickListener(this);
		text.setText(String.format(context.getResources().getString(R.string.update_content), version,desc));
		
		linearLayout.setGravity(Gravity.CENTER);
		setContentView(linearLayout, new ViewGroup.LayoutParams(Utils.getScreenWidth(context)-50, LayoutParams.WRAP_CONTENT));

		Window window = this.getWindow();
		window.setBackgroundDrawableResource(android.R.color.transparent);
		LayoutParams params = window.getAttributes();// 获取LayoutParams
		window.setGravity(Gravity.CENTER);
		window.setAttributes(params);// 设置生效
	}

	@Override
	public void show() {
		super.show();
	}

	public UpdateDialog setPositiveButton(OnClickListener onClickListener) {
		this.onPositiveClickListener = onClickListener;
		return this;
	}
	public UpdateDialog setNegativeButton(OnClickListener onClickListener) {
		this.onNegativeClickListener = onClickListener;
		return this;
	}
	
	
	public interface OnClickListener {
		void onClick(DialogInterface dialog,int which);
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			onPositiveClickListener.onClick(this, R.id.button1);
			break;
		case R.id.button2:
			onNegativeClickListener.onClick(this, R.id.button2);
			break;
		default:
			break;
		}
	}
}
